// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Author: Markus Gutschke

#include <stdlib.h>
#include <string.h>

#include "check.h"
#include "list_printers.h"
#include "http.h"
#include "multipart.h"
#include "stringprintf.h"


struct JSON *listPrinters(const char *url, struct OAuth2 *oauth2,
                          const char *proxyName) {
  // Construct "list" URL
  char *api = stringPrintf(NULL, "%s/list", url);

  // Send the list request
  struct MultiPart multipart;
  initMultiPart(&multipart);
  addToMultiPart(&multipart, "proxy", proxyName, -1, "text/plain");
  const char *body = POST(api, oauth2, &multipart, M_MULTIPART);
  free(api);
  destroyMultiPart(&multipart);

  // Parse response
  struct JSON *json = newJSON(body);
  free((void *)body);

  // Verify that the fetch request was successful
  check(json->type == J_OBJECT);
  const struct JSON *success;
  check((success = (const struct JSON *)
         getFromHashMap(&json->obj, "success")) &&
        success->type == J_TRUE);

  return json;
}
